package com.gxa.clientarticle.controller;

/*
 *
 *@author leijun
 *
 *@creating 2020/11/13 10:10
 *
 */

import com.gxa.clientarticle.exception.BusinessException;
import com.gxa.clientarticle.pojo.Article;
import com.gxa.clientarticle.service.ArtcleService;
import com.gxa.clientarticle.utils.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import rx.internal.operators.BackpressureUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @ClassName ArtcleController
 * @Description TODO
 * @Author 俊
 * @Date 2020/11/13 10:10
 * @Version 1.0
 */
@RestController
@RequestMapping("/art")
public class ArtcleController {

    @Resource
    ArtcleService artcleService;

    @RequiresPermissions("18")
    @PostMapping("/add")
    public Map add(@RequestBody Article article){
        JsonResult jsonResult = new JsonResult();
        Integer add = artcleService.add(article);
        if (add != 1){
            throw new BusinessException("添加失败");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getValues();

    }

    @RequiresPermissions("18")
    @PostMapping("/list")
    public Map list(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit){
        JsonResult jsonResult = new JsonResult();
        Map list = artcleService.list(page, limit);
        if(list == null){
            throw new BusinessException("查询失败");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.put("list",list.get("list"));
        jsonResult.put("count",list.get("count"));
        return jsonResult.getValues();
    }

    @RequiresPermissions("18")
    @PostMapping("/listSort")
    public Map typeList(@RequestParam("type") Integer type,@RequestParam("page") Integer page,@RequestParam("limit") Integer limit){
        JsonResult jsonResult = new JsonResult();
        Map list = artcleService.typeList(type, page, limit);
        if(list == null){
            throw new BusinessException("查询失败");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.put("list",list.get("list"));
        jsonResult.put("count",list.get("count"));
        return jsonResult.getValues();
    }
    @RequiresPermissions("18")
    @PostMapping("/listTitle")
    public Map listTitle(@RequestParam("name") String name,@RequestParam("page") Integer page,@RequestParam("limit") Integer limit){
        JsonResult jsonResult = new JsonResult();
        Map list = artcleService.listTitle(name, page, limit);
        if(list == null){
            throw new BusinessException("查询失败");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.put("list",list.get("list"));
        jsonResult.put("count",list.get("count"));
        return jsonResult.getValues();
    }
    @RequiresPermissions("18")
    @PostMapping("/update")
    public Map update(@RequestBody Article article){
        JsonResult jsonResult = new JsonResult();
        Integer update = artcleService.update(article);
        if (update != 1){
            throw new BusinessException("修改失败");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getValues();

    }
    @RequiresPermissions("18")
    @PostMapping("/updateState")
    public Map updateState(@RequestParam("id") Integer id,@RequestParam("state") Boolean state){
        JsonResult jsonResult = new JsonResult();
        Integer updateState = artcleService.updateState(id, state);
        if (updateState != 1){
            throw new BusinessException("修改失败");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getValues();

    }
    @RequiresPermissions("18")
    @PostMapping("/del")
    public Map delete(@RequestParam("id") Integer id){
        JsonResult jsonResult = new JsonResult();
        Integer delete = artcleService.delete(id);
        if (delete != 1){
            throw new BusinessException("删除失败");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getValues();

    }




}
